package utils

import (
	"com.yy.api/common/logger"
	"crypto/rand"
	"encoding/base64"
	"encoding/binary"
	"time"
)

func GenerateDisplayID() (string, bool) {
	timestamp := uint32(time.Now().Unix())
	b := make([]byte, 11) // 11 bytes -> 88 bits
	_, err := rand.Read(b)
	if err != nil {
		logger.Default.Errorf("%v", err)
		return "", false
	}

	// 前4字节放时间戳（保证部分有序）
	copy(b[:4], []byte{byte(timestamp >> 24), byte(timestamp >> 16), byte(timestamp >> 8), byte(timestamp)})

	return base64.RawURLEncoding.EncodeToString(b)[:15], true
}

func GenerateTimestampBasedUserID() uint64 {
	timestamp := uint32(time.Now().Unix())
	var random uint32
	binary.Read(rand.Reader, binary.LittleEndian, &random)
	return uint64(timestamp) | uint64(random)
}
